package com.mach.platform.sprider.dev.virdev;


import com.mach.platform.sprider.Conf;
import com.mach.platform.sprider.dev.ByteUtil;
import com.mach.platform.sprider.dev.DataHelper;
import com.mach.platform.sprider.dev.UU;
import com.mach.platform.util.SqlHelper;
import org.joda.time.DateTime;

import java.io.IOException;
import java.util.Map;

public class LogoMbTcpDev extends DataHelper {

    protected String devName;

    protected SqlHelper sqls;
    private Map<String, Object> entData;
    private Map<String, Object> tetData;

    public LogoMbTcpDev(String ip, Integer port, Integer devID, String devName) {
        super(ip,port,devID);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;
    }

    @Override
    public void run(){

        long preSec = 0;
        long nowSec = 0;
        while(true){


            try {

               /* long nowMill = System.currentTimeMillis();

                nowSec = nowMill / 1000;

                if(nowSec == preSec)continue;

                Date now = new Date(nowMill);

                int fyear = now.getYear() + 1900;
                int fmonth = now.getMonth() + 1;
                int fday = now.getDate();
                int fhour = now.getHours();
                int fmin = now.getMinutes();
                int fsec = now.getSeconds();*/

                DateTime dateTime = new DateTime();
                int fyear = dateTime.getYear();
                int fmonth = dateTime.getMonthOfYear();
                int fday = dateTime.getDayOfMonth();
                int fhour = dateTime.getHourOfDay();
                int fmin = dateTime.getMinuteOfHour();
                int fsec = dateTime.getSecondOfMinute();

            }finally {

            }

            preSec = nowSec;


        }


    }

    /*
         冷冻水:供水温度
      */
    public Float getCoolWaterImportTemp() throws IOException {
        Float result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(4, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = ByteUtil.getFloat(data);

        return result;
    }

    /*
        冷冻水:回水温度
     */
    public Float getCoolWaterBackTemp() throws IOException {
        Float result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(40, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = ByteUtil.getFloat(data);

        return result;
    }
    /*
        冷冻水:累计流量
     */
    public Integer getCoolWaterSumFlow() throws IOException {
        Integer result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(8, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
        //data = ByteUtil.reverse2Byte(data);
        //data = ByteUtil.reverse(data);
        ByteUtil.reveIn2Byte(data, 0);
        ByteUtil.reveIn2Byte(data, 2);
        result = ByteUtil.getInt(data);

        return result;
    }

    /*
      冷冻水:累计冷量
   */
    public Integer getCoolWaterSumCool() throws IOException {
        Integer result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(12, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        ByteUtil.reveIn2Byte(data, 0);
        ByteUtil.reveIn2Byte(data, 2);
        result = ByteUtil.getInt(data);

        return result;
    }

    /*
     冷冻水:test累计冷量
  */
    public Integer testCoolWaterSumCool() throws IOException {
        Integer result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(12, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        result = ByteUtil.getInt(data);

        return result;
    }
    public Integer getInt(Integer startAddr,Integer  len) throws IOException {
        Integer result = null;

        byte[] repData = this.getAddrData(startAddr, len);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        result = ByteUtil.getInt(repData);

        return result;
    }
    public Short getShort(Integer startAddr,Integer  len) throws IOException {
        Short result = null;

        byte[] repData = this.getAddrData(startAddr, len);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        byte[] databuf = ByteUtil.reverse(repData);
         result = ByteUtil.getShort(databuf);

        return result;
    }

    public byte[] getAddrData(Integer startAddr,Integer  len) throws IOException {
        byte[] result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(startAddr, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,len);
        result = data;
        UU.printData(result);
        return result;
    }


    public static void main(String[] args) throws IOException {
        LogoMbTcpDev pr2 = new LogoMbTcpDev("192.168.0.182",503,1,"PR2车间");
        Short result1 = pr2.getShort(8, 2);
        System.out.println("data=" +result1);
        Short result2 = pr2.getShort(9, 2);
        System.out.println("data=" +result2);
        Short result3 = pr2.getShort(7, 2);
        System.out.println("data=" +result3);



    }


}
